$(function () {
	var host ='https://cloud.kaaction.com/crab/api/v1/';
	var server = "https://cloud.kaaction.com";
	// 获取兑换信息
	var cardpas = sessionStorage['cardpassword'] ;
	var cardn = sessionStorage['cardnumber'] ;
	var resinfomation = '';
	var choosecrabwidth = '';
	var choosecrabimgwidth = '';
	var choosecrab = '';
	$.ajax({
		url:host + 'crabExchangeCouponApi/selExchangeCouponProduct',
		dataType:'json',
		data:{
			cardNum:cardn,
			password:cardpas
		},
		success:function(res){
			resinfomation = res.data;
			console.log(resinfomation)
			$('span.usercard').html(resinfomation.cardNum);
			$('span.userpaasword').html(resinfomation.password);
			$.each(resinfomation.listExchangeCouponProduct,function(key,value){
				var $alink  = $('<a>');
				$alink.attr('href','javascript:void(0);');
				var $div = $('<div id='+value['id']+'></div>');
				var $divcrab = $('<div>');
				var $divblack = $('<div>');
				$divblack.append('<img src="./image/check.png" class="check">')
				$divblack.addClass('blackback show');
				$alink.addClass('tocarb');
				$div.addClass('carb-content');
				$divcrab.addClass('carb-des');
				$alink.href = value['link'];
				$divcrab.append('<p>'+ value['name'] +'</p>')
						.append('<p>'+ value['male'] +'</p>')
						.append('<p>'+ value['female'] +'</p>')
				$div.append('<img src='+server + value['photo'] +'></img>')
					.append($divcrab)
					.append($divblack)
					.appendTo($alink);
				$('div.crablist').append($alink);
			})
			choosecrabwidth = $('div.carb-content').height();
			choosecrabimgwidth = $('div.carb-content img').width();
			choosecrab = choosecrabwidth + choosecrabimgwidth;
			$('div.blackback').css('width',choosecrabimgwidth + 'px');
			$('div.blackback').css('height',choosecrab + 'px');			
		}
	})
	// 选择螃蟹种类
	var productId = '';
	$(document).on('click','div.carb-content',function(){
		$(this).find('div.blackback').toggleClass('show');
		$(this).parent('a.tocarb').siblings('a').find('div.blackback').addClass('show');
		if($(this).find('div.blackback').hasClass('show')){
			productId = '';
		}else{
			productId = $(this).attr('id');
		}	
		console.log(productId)
	})
	// 选择期望收获时间
	var dd = new Date(); 
	dd.setDate(dd.getDate()+1);//获取AddDayCount天后的日期
	$("#wantdate").mobiscroll().date({
	    theme: 'android-ics light', //皮肤样式
	    display: 'bottom',
	    lang: 'zh',
	    dateFormat: 'yyyy-mm-dd',
	    minDate: dd
    });
	// 选择省份	
	$.ajax({
		url: host + 'crabAreaApi/getProvince',
	    dataType:'json',
	    success:function(res){
	    	console.log(res.data)
	    	var provinceresult = res.data;
	    	$('#provincelist').empty();
	    	$.each(provinceresult,function(key,value){
	    		var $li = $('<li data-value='+value.code+'>'+value.name+'</li>') ;
	    		$li.appendTo('#provincelist');
	    	})
	    }
	})	
	var provincecode ='';
	$('#province').click(function(){
		$('#provincelist').mobiscroll().treelist({
			theme: "android-ics light",  
            lang: "zh",  
            display: 'bottom', 
            inputClass: 'tmp',
            headerText: '请您选择',
            onSelect:function(valueText,inst){
            	console.log(valueText);
            	var provincecontent = $('#provincelist').children('li').eq(valueText).text();
            	$('#province').val(provincecontent);
            	provincecode = $('#provincelist').children('li').eq(valueText).attr('data-value');
            	console.log(provincecode);
            	// 根据省份获取城市信息
            	$.ajax({
            		url: host + 'crabAreaApi/getCity',
				    dataType:'json',
				    data:{
				    	province:provincecode
				    },
				    success:function(res){
				    	console.log(res.data)
				    	var provinceresult = res.data;
				    	$('#citylist').empty();
				    	$.each(provinceresult,function(key,value){
				    		var $li = $('<li data-value='+value.code+'>'+value.name+'</li>') ;
				    		$li.appendTo('#citylist');
				    	})
				    }
            	})
            }
		})
		$("input[id^=provincelist]").focus();
	})
	// 选择城市
	var citycode ='';
	$('#city').click(function(){
		$('#citylist').mobiscroll().treelist({
			theme: "android-ics light",  
            lang: "zh",  
            display: 'bottom', 
            inputClass: 'tmp',
            headerText: '请您选择',
            onSelect:function(valueText,inst){
            	console.log(valueText);
            	var citycontent = $('#citylist').children('li').eq(valueText).text();
            	$('#city').val(citycontent);
            	citycode = $('#citylist').children('li').eq(valueText).attr('data-value');
            	console.log(citycode);
            	// 根据省份获取城市信息
            	$.ajax({
            		url: host + 'crabAreaApi/getCounty',
				    dataType:'json',
				    data:{
				    	city:citycode
				    },
				    success:function(res){
				    	console.log(res.data)
				    	var arearesult = res.data;
				    	$('#countrylist').empty();
				    	$.each(arearesult,function(key,value){
				    		var $li = $('<li data-value='+value.code+'>'+value.name+'</li>') ;
				    		$li.appendTo('#countrylist');
				    	})
				    }
            	})
            }
		})
		$("input[id^=citylist]").focus();
	})
	// 选择区域
	var citycode ='';
	$('#area').click(function(){
		$('#countrylist').mobiscroll().treelist({
			theme: "android-ics light",  
            lang: "zh",  
            display: 'bottom', 
            inputClass: 'tmp',
            headerText: '请您选择',
            onSelect:function(valueText,inst){
            	console.log(valueText);
            	var areacontent = $('#countrylist').children('li').eq(valueText).text();
            	$('#area').val(areacontent);
            	areacode = $('#countrylist').children('li').eq(valueText).attr('data-value');
            	console.log(areacode);
            }
		})
		$("input[id^=countrylist]").focus();
	})
	// 获取地址信息
	$("#usertel").blur(function(){
		if($(this).val().match(/^1([358][0-9]|4[57]|7[0135678])\d{8}$/) == null){
			tel = false;
			$('span.warning').text('请输入正确的手机卡号！');
		}else{
			tel = true;
			$('span.warning').text('');
		}
	})
		var username = '';
		var usertel = '';
		var province = '';
		var city = '';
		var area = '';
		var detailadd = '';
		var wantdate = '';
		var note = '';
	$('.refer img').click(function(){
		username = $('#username').val();
		usertel = $('#usertel').val();
		province = $('#province').val();
		city = $('#city').val();
		area = $('#area').val();
		detailadd = $('#detailadd').val();
		wantdate = $('#wantdate').val();
		note = $('#note').val();
		if(note == ''){
			note = '无'
		}
		if(username && usertel && province && city && area && detailadd && wantdate && tel && productId){
			$('.sure').removeClass('show');
			$('.surelistusername').html(username);
			$('.surelistusertel').html(usertel);
			$('.surelistaddress').html(province+city+area+detailadd);
			$('.surelistwantdate').html(wantdate);
			$('.surelistnote').html(note);
			$('span.warning').text('');
			ScreenScroll();
		}else{
			$('span.warning').text('请将信息输入正确！');
		}	
	})
	// 提交信息确认
	$('img.imgsure').click(function(){
		$.ajax({
			url:host + 'crabExchangeCouponApi/orderCreate',
			dataType:'json',
			data:{
				name:username,
				mobile:usertel,
				province:provincecode,
				city:citycode,
				county:areacode,
				address:detailadd,
				hopeDate:wantdate,
				remarks:note,
				cardNum:cardn,
				password:cardpas,
				productId:productId
			},
			success:function(res){
				if(res.err_code == 'success'){
					$('#username').val('');
					$('#usertel').val('');
					$('#province').val('');
					$('#city').val('') ;
					$('#area').val('');
					$('#detailadd').val('');
					$('#wantdate').val('');
					$('#note').val('');
					window.location.href = 'success.html';
					$('span.warning').text('');
				}else{
					$('span.warning').text(res.err_msg);					
				}				
			}
		})
	})
	// 提交信息取消
	$('img.imgcancel').click(function(){
		$('.sure').addClass('show');
		ScreenScroll();
	})
	$('img.imgsure').click(function(){
		$('.sure').addClass('show');
		ScreenScroll();
	})
	// 弹款出现背景不滚动
	function ScreenScroll(){
		if($('.sure').hasClass('show')){
			$('body').css({
				'overflow':'visible'
			})
		}else{
			$('body').css({
				'overflow':'hidden'
			})
		}
	}
});






